from django.shortcuts import render, HttpResponse, redirect, reverse
from .models import StudentInformationModel, CourseModel
# Create your views here.
from blueking.component.shortcuts import get_client_by_request
from django.http import JsonResponse
# 主界面
def index(request):
    statuses = StudentInformationModel.objects.all()
    return render(request, 'index.html', {'statuses': statuses})
def course(request):
    statuses = CourseModel.objects.all()
    return render(request, 'course.html', {'statuses': statuses})

def addcourse(request):
    if request.method == "POST":
        print("shssss")
        statuses =  CourseModel.objects.all()
        cour_id = request.POST.get('cour_ida')
        course = request.POST.get('coursea')
        grade = request.POST.get('gradea')
        if(StudentInformationModel.objects.filter(stu_id=cour_id)):
            stu_data =  CourseModel()
            stu_data.cour_id = cour_id
            stu_data.course = course
            stu_data.grade = grade
            stu_data.save()
            return JsonResponse({"result": True, "code": 200, "message": "success"})
        else:

            return JsonResponse({"result": False, "code": 101, "message": "error,学号不存在"})
    else:
        return JsonResponse({"result": False, "code": 101, "message": "false"})
# 登录界面



# 增加数据 增加只能root用户或者管理员才能操作
def add(request):
    if request.method == "POST":
        print("shaxi3d")
        statuses = StudentInformationModel.objects.all()
        stu_id = request.POST.get('stu_id')
        stu_name = request.POST.get('stu_name')
        posttype=request.POST.get('posttype')

        if(StudentInformationModel.objects.filter(stu_id=stu_id)):
            context = {
            'error': '增加失败,此用户已存在',
            'statuses': statuses,
            }
            if(posttype=="ajax"):
                return JsonResponse({"result": False, "code": 101, "message": "error,学号已存在"})
            else:
                return render(request, 'index.html', context)
        
        else:
        
            stu_phone = request.POST.get('stu_phone')
            stu_addr = request.POST.get('stu_addr')
            stu_faculty = request.POST.get('stu_faculty')
            stu_major = request.POST.get('stu_major')
            stu_data = StudentInformationModel()
            stu_data.stu_id = stu_id
            stu_data.stu_name = stu_name
            stu_data.stu_phone = stu_phone
            stu_data.stu_addr = stu_addr
            stu_data.stu_faculty = stu_faculty
            stu_data.stu_major = stu_major
            stu_data.stu_course_id="1"
            stu_data.save()

            context = {
                'sucess': '增加成功',
                'statuses': statuses,
            }
        if(posttype=="ajax"):
            return JsonResponse({"result": True, "code": 200, "message": "success"})
        else:
            return render(request, 'index.html', context)
    else:
        return render(request, 'index.html')



# 查询
def select(request):
    if request.method == "POST":
        print("shaxi")
        id = request.POST.get('stu_id')
        stu_data = StudentInformationModel.objects.filter(stu_id__contains=id)
        return render(request, 'select.html', {'statuses': stu_data})
        
    else:
        context = {
            'msg': False,
            
        }
        return render(request, 'select.html', context)

def deletecourse(request):
    if request.method == "POST":
        print("shaxi")
        statuses =  CourseModel.objects.all()
        id = request.POST.get('cour_id')
        CourseModel.objects.filter(cour_id=id).delete()
        context = {
            'sucess': '删除成功',
            'statuses': statuses,
        }
        # return redirect('/course/')
        return redirect('/o/myfirst/course/')
    else:
        context = {
            'error': '删除失败',
           'statuses': statuses,
        }
        return render(request, 'course.html', context)
# 删除
def delete(request):
    if request.method == "POST":
        print("shaxi")
        id = request.POST.get('stu_id')
        StudentInformationModel.objects.filter(stu_id=id).delete()
        statuses = StudentInformationModel.objects.all()
        context = {
            'sucess': '删除成功',
            'statuses': statuses,
        }
        return render(request, 'index.html', context)
    else:
        context = {
            'error': '删除失败',
           'statuses': statuses,
        }
        return render(request, 'index.html', context)



# 修改
def update(request):
    
    if request.method == "POST":
        print("post")
        stu_id = request.POST.get('stu_id')
        stu_name = request.POST.get('stu_name')
        stu_phone = request.POST.get('stu_phone')
        stu_addr = request.POST.get('stu_addr')
        stu_faculty = request.POST.get('stu_faculty')
        stu_major = request.POST.get('stu_major')
        print(stu_addr)
        #StudentInformationModel.objects.filter(stu_id=id).update(stu_id=stu_id, stu_name=stu_name, stu_phone=stu_phone, stu_addr=stu_addr, stu_faculty=stu_faculty, stu_major=stu_major)
        # 或者 以下这种，对单个数据进行修改
        stu_data = StudentInformationModel.objects.get(stu_id=stu_id)
        stu_data.stu_id = stu_id
        stu_data.stu_name = stu_name
        stu_data.stu_phone = stu_phone
        stu_data.stu_addr = stu_addr
        stu_data.stu_faculty = stu_faculty
        stu_data.stu_major = stu_major
        stu_data.save()
        statuses = StudentInformationModel.objects.all()
        receiver = stu_phone

        title = f"管理员修改了你的信息"
        content = f"管理员修改了你的信息：姓名：{stu_data.stu_name}\n住址：{stu_data.stu_addr}\n院系：{stu_data.stu_faculty}\n专业：{stu_data.stu_major}"
        client = get_client_by_request(request)
        send_result = client.cmsi.send_mail({
        "receiver": receiver,
        "title": title,
        "content": content,
        })
        if send_result["result"]:
            print("nice")
            context = {
                'sucess': '修改成功',
                'statuses': statuses,
            }
            return render(request, 'index.html', context)
        else:
        # 发送邮件失败
            print("xxx")
            return render(request, 'update.html', {'result': send_result["message"]})
    else:
        print("Sss")
        stu_id = request.GET.get('id')
        email = request.GET.get('email')

        print(stu_id)
        #StudentInformationModel.objects.filter(stu_id=id).update(stu_id=stu_id, stu_name=stu_name, stu_phone=stu_phone, stu_addr=stu_addr, stu_faculty=stu_faculty, stu_major=stu_major)
        # 或者 以下这种，对单个数据进行修改
        stu_data = StudentInformationModel.objects.get(stu_id=stu_id)

        stu_phone=stu_data.stu_phone 
        statuses = StudentInformationModel.objects.all()
        receiver = stu_phone

        title = f"管理员通知"
        content = f"管理员通知：{email}"
        client = get_client_by_request(request)
        send_result = client.cmsi.send_mail({
        "receiver": receiver,
        "title": title,
        "content": content,
        })
        if send_result["result"]:
            print("nice")
            context = {
                'sucess': '发送成功',
                'statuses': statuses,
            }
            return render(request, 'index.html', context)
        else:
        # 发送邮件失败
            print("xxx")
            return render(request, 'update.html', {'result': send_result["message"]})



def updatecourse(request):
    print("nice")
    if request.method == "POST":

        statuses =  CourseModel.objects.all()
        cour_id = request.POST.get('cour_id')
        course = request.POST.get('course')
        grade = request.POST.get('grade')

        stu_data =  CourseModel.objects.get(cour_id=cour_id)
        stu_data.cour_id = cour_id
        stu_data.course = course
        stu_data.grade = grade
        stu_data.save()

        print("nice")
        context = {
            'sucess': '修改成功',
            'statuses': statuses,
        }
        return redirect('/o/myfirst/course/')
